/**
 * Created by cchen on 2015/5/11.
 */
//var iscroll = new iScroll("body", { "useTransition": true, "vScrollbar": false, "hScroll": false, "onScrollStart": function () {
//    //scrollControl = true;
//}})
var params1 = "",deadline,timeout = false,
    activityType;//团购或非团购

$(document).ready(function(){
    var params = {};
    if (window.location.pathname) {
        var parts = location.search.substring(1).split('&');
        for (var i = 0; i < parts.length; i++) {
            var nv = parts[i].split('=');
            if (!nv[0]) continue;
            params[nv[0]] = nv[1] || true;
        }
    }
    //参加列表
    var listHandler = function(data){
        var html = "",d;
        
        for(var i = 0;i < data.length;i++){
              if(data[i].number){
                  //d = new Date(data[i].date);
                  if(i>=5)break;
                  html += "<span class = 'img-v'><img src='"+data[i].images+"' onerror = 'imageError(this)'/>"+(data[i].objId?"<img src = '/pages/static/mobile/images/v.png' class = 'vcf'>":"")+"</span>";
              }else{
                  d = new Date(data[i].replyDate);
                  html += "<li><div class = 'portrait-title'><span class = 'img-hv'><img src='"+data[i].images+"' onerror = 'imageError(this)'/><img src = '/pages/static/mobile/images/v.png'></span><div class = 'portrait-left'><span>"+ data[i].nickName +"</span><br><span>"+ d.getFullYear() +"-"+ (d.getMonth()+1) +"-"+ d.getDate() + " " + d.getHours() +":"+ d.getMinutes() +":"+ d.getSeconds() +"</span></div></div><div class = 'clear'></div><div class = 'discuss-content ell'>"+ data[i].replyDesc +"</div></li>";
              }
        }
        return html;
    }
    $("#neighbor_list").on("click",function(){
        location.href = "/pages/static/mobile/partials/share/web/list.html?activityId="+params1;
    });
    var imageError = function(c){
        c.src = '/pages/static/mobile/images/portrait_normal.png';
        };
    
 
    $.ajax({url:"/rest/v1.0/mobile/web/share/"+ params["shareId"] +"/"+params["userId"],async:false,
        success: function(res) {
            if(res.data.obj) {
                activityType = res.data.obj.activityType;
                deadline = res.data.obj.deadline;
                params1 = res.data.obj.objId;
                $("#title").html(res.data.obj.title);
                var d = new Date(res.data.obj.dateCreated),
                    createDate = d.getFullYear() +"-"+ numTowDisplay(d.getMonth()+1) +"-"+ numTowDisplay(d.getDate()) + " " + numTowDisplay(d.getHours()) +":"+ numTowDisplay(d.getMinutes()) +":"+ numTowDisplay(d.getSeconds());
                $("#createDate").html(createDate);
                $("#content").html(res.data.obj.desc);
                $("#village_name").html(res.data.obj.communityName);
                $("#launch").html(res.data.obj.titleDesc);
                $("#village_activity").html(res.data.obj.categoryName);
                $("#join-reply").html(res.data.obj.replyCount);//回复数
                $("#join-num").html(res.data.obj.number);//参与人数
                $("#writer_title").html(res.data.obj.title);//分享内容标题
                if(res.data.obj.createdBy)$("#writer").html(res.data.obj.createdBy.nickName);//创建者名称
                $("#write_time").html(createDate);//创建时间
                $("#look_num").html(res.data.obj.browserCount);//浏览量
                $("#writer_content").html(res.data.obj.desc);//内容描述
                if(res.data.obj.createdBy)$("#writer_portrait").attr("src",res.data.obj.createdBy.images);//分享人头像

                //讨论
                if(Number(res.data.obj.type) == 1){
                     if(res.data.obj.replies&&res.data.obj.replies.length != 0){
                         $("#reply_list").html(listHandler(res.data.obj.replies));
                     }else{
                         $(".join-discuss").css("display","none");
                     }
                    $("#neighbor_list").css("display","none");
                    $("#join_tip").css("display","none");
                    $("#header").css("display","none");
                    $("#content").css("display","none");
                }


                //活动
                if(Number(res.data.obj.type) == 2){
                     if(res.data.obj.partake&&res.data.obj.partake.length != 0){
                         $("#img_list").html(listHandler(res.data.obj.partake));
                         $("#neighbor_num").text(res.data.obj.partakeNum);
                     }else{
                         $("#neighbor_list").css("display","none");
                         $("#no_neighbor").css("display","block");
                     }

                    if(res.data.obj.replies&&res.data.obj.replies.length != 0){
                        $("#reply_list").html(listHandler(res.data.obj.replies));
                    }else{
                        //$("#writer_area").css("display","none");
                        $(".join-discuss").css("display","none");
                    }

                    $("#join_tip").text("我要参加");
                    //$("#end_time").text("距离结束");
                    var now = new Date().getTime();
                    if(deadline > now){
                        //$("#end_time").html("<span style = 'color: #FB7250;'>"+numTowDisplay(Math.ceil((deadline - now)/86400))+"</span>天");
                    }else{
                        $("#end_time").text("活动结束");
                        $("#join_tip").text("活动结束");
                        $("#join_tip")[0].style.background = "#A8A8A8";
                        timeout = true;
                    }
                    $("#header").css("display","none");
                    $("#content").css("display","none");
                    if(!activityType){
                        $("#join_tip").css("display","none");
                    }
                }



                //专题
                if(Number(res.data.obj.type) == 3){
                    $("#join_tip").css("display","none");
                    $("#writer_area").css("display","none");
                    $("#neighbor_list").css("display","none");
                    $(".join-discuss").css("display","none");
                }


                var galary = $("#galary");
                var images = res.data.obj.images;
                if (images && images.length > 0) {
                    for (var i in images) {
                        galary.append("<img class='pic'  src='" + res.data.host + images[i].imageURL + "' />");
                    }
                }
                //iscroll.refresh();
                //$("img").on("load",function(){iscroll.refresh();});
                //$("img").on("error",function(){iscroll.refresh();});
            }
            else{
                $("#title").html("无法显示网页");
            }

        },error:function(res) {
              $("#title").html("无法显示网页");
        }

    });


});

(function (w) {
    var share = function () {
        this.Init();
        return this;
    },codeControl = true,
        interval,count = 0;
    share.prototype = {
        Init: function () {
            //document.getElementById("join_input").style.height = window.body.height + "px";
            var ua = navigator.userAgent.toLowerCase();
            if(ua.match("iphone") == "iphone"){
                $("#input_code")[0].style.webkitAppearance = "caret";
                $("#remark")[0].style.webkitAppearance = "caret";
            }
        },
        listHandle: function(data){
            var d = eval( "(" + data + ")"),html = "";

        },
        timeControl: function(data){
            var d = eval( "(" + data + ")");
            if(d.result.status == 1){
               $("#get_code").html("重新获取<span id = 'count_down'>60</span>");
               $("#get_code").addClass("get-code1");
                interval = setInterval(function(){
                    count++;
                    var num = 60 - count;
                    if(num < 0){
                        $("#get_code").removeClass("get-code1");
                        codeControl = true;
                        clearInterval(interval);
                        num = "";
                        count = 0;
                    }
                    $("#count_down").text(num);
               },1000);
            }
        },
        joinSuccess: function(data){
            var d = eval( "(" + data + ")");
            if(d.result.status == 1){
                $("#toast").text("参加活动成功！");
                $("#toast").addClass("toasts");
            }else{
                $("#toast").text(d.result.message);
                $("#toast").addClass("toasts");
            }
        },
        evt: function() {
            var c = this;
            $("#down_num").on("touchend", function () {
                if ($("#join_num").val() < 3) {
                    $("#join_num").val(1);
                    $("#join_num_control").addClass("join-num-control1");
                } else {
                    $("#join_num_control")[0].className = "join-num-control";
                    var cn = Number($("#join_num").val()) - 1;
                    $("#join_num").val(cn);
                }
            });

            $("#up_num").on("touchend", function () {
                if($("#join_num").val() < 99){
                    $("#join_num_control")[0].className = "join-num-control";
                    var cn = Number($("#join_num").val()) + 1;
                    $("#join_num").val(cn);
                }else{
                    $("#join_num_control").addClass("join-num-control2");
                }
            });

            $("#button_left").on("click",function(){
                $("#join_layer").css("display","none");
                $("#mask").css("display","none");
            });

            $("#button_right").on("click",function(){
                var note = $("#remark").val(),
                    name = $("#join_name").val(),
                    address = $("#address").val();
                if($("#join_num").val() == 0){
                    $("#toast").text("请选择份数！");
                    $("#toast").addClass("toasts");
                    return;
                }
                //备注字数限制
                if(note.length > 8){
                    note = note.substring(0,8);
                }

                if($("#join_name").val().replace(/\s/g, "") == ""){
                    $("#join_name").css("border","1px solid red");return;
                }else if(name > 5){
                    name = name.substring(0,5);
                }
                if(!/^[1][3-9][0-9]{9,9}$/.test($("#join_phone").val())){
                    $("#join_phone").css("border","1px solid red");return;
                }
                if($("#input_code").val().replace(/\s/g, "") == ""){
                    $("#input_code").css("border","1px solid red");return;
                }
                if(activityType == 1 && $("#address").val().replace(/\s/g, "") == ""){
                    $("#address").css("border","1px solid red");return;
                }
                if(address.length > 30){
                    address = address.substring(0,30);
                }
                $("#join_layer").css("display","none");
                $("#mask").css("display","none");
                $("#button_right").removeClass("button-right1");

                c.data = {"url":"/rest/v1.0/mobile/shares/activities/joinInActivityByWechat","method":"post","data":"activityId="+params1+"&number="+$("#join_num").val()+"&name="+name+"&note="+note+"&phone="+$("#join_phone").val()+"&messageCode="+$("#input_code").val()+"&address="+address};
                netWork(c.data,c.joinSuccess);
            });

            $("#join_tip").on("click",function(){
                if(timeout)return;
                if(activityType == 2){
                    $("#address").css("display","none");
                }
                $("#join_layer").css("display","block");
                $("#mask").css("display","block");
                $("#body")[0].scrollTop = 0;
            });

           document.getElementById("toast").addEventListener('webkitAnimationEnd', function(){
                this.className = "toast";
            }, false);

            $("#get_code").on("click",function(){
                if(codeControl){
                    if(!/^[1][3-9][0-9]{9,9}$/.test($("#join_phone").val())){
                        $("#join_phone").css("border","1px solid red");
                        return;
                    }
                    codeControl = false;
                    c.data = {"url":"/rest/v1.0/mobile/shares/activities/getActivityMessageCode/"+$("#join_phone").val()};
                    netWork(c.data,c.timeControl);
                }
            });

            $(".join-input").on("input",function(){
                var value = this.value.replace(/\s/g, "");
                if(value != ""){
                    if(this.id == "join_name") {
                        $("#join_name").addClass("join-name1");
                        $("#name_tip").css("display","block");
                        $("#name_tip").text($("#join_name").val().length+"/5");
                        $("#name_timg")[0].style.display = "block";
                        if($("#join_name").val().length > 5){
                            $("#join_name").css("border","1px solid red");
                        }else{
                            $("#join_name").css("border","1px solid #EFEBEC");
                        }
                    }
                    if(this.id == "join_phone" && /^[1][3-9][0-9]{9,9}$/.test(value)){
                        $("#phone_timg")[0].style.display = "block";
                        $("#join_phone").css("border","1px solid #EFEBEC");
                    }else{
                        $("#phone_timg")[0].style.display = "none";
                    }
                    if(this.id == "input_code"){
                        $("#code_timg")[0].style.display = "block";
                        $("#input_code").css("border","1px solid #EFEBEC");
                    }
                    if(this.id == "address"){
                        $("#address").addClass("address1");
                        $("#address_tip").css("display","block");
                        $("#address_tip").text($("#address").val().length+"/30");
                        if($("#address").val().length > 30){
                            $("#address").css("border","1px solid red");
                        }else{
                            $("#address").css("border","1px solid #EFEBEC");
                        }
                    }
                    //备注字数限制
                    if(this.id == "remark"){
                        $("#remark").addClass("remark1");
                        $("#word_tip").css("display","block");
                        $("#word_tip").text($("#remark").val().length+"/8");
                        if($("#remark").val().length > 8){
                            $("#remark").css("border","1px solid red");
                        }else{
                            $("#remark").css("border","1px solid #EFEBEC");
                        }
                    }

                    if($("#join_name").val().replace(/\s/g, "") != "" && /^[1][3-9][0-9]{9,9}$/.test($("#join_phone").val()) && $("#input_code").val().replace(/\s/g, "") != ""){
                        if(activityType == 2){
                            $("#button_right").addClass("button-right1");
                        }else if($("#address").val().replace(/\s/g, "") != ""){
                            $("#button_right").addClass("button-right1");
                        }
                    }
                }else{
                    if(this.id == "join_name") {
                        $("#name_timg")[0].style.display = "none";
                        $("#join_name").removeClass("join-name1");
                        $("#name_tip").css("display","none");
                    }
                    if(this.id == "join_phone"){
                        $("#phone_timg")[0].style.display = "none";
                    }
                    if(this.id == "input_code"){
                        $("#code_timg")[0].style.display = "none";
                    }
                    //备注字数限制
                    if(this.id == "remark"){
                        $("#remark").removeClass("remark1");
                        $("#word_tip").css("display","none");
                    }
                    if(this.id == "address"){
                        $("#address").removeClass("address1");
                        $("#address_tip").css("display","none");
                    }
                }
            });
        }
    };
    w.share = share;
})(window);
//活动
    new share().evt();

